<%content_for :title do%><%= @category.title %><%end%>
<%content_for :keywords do%><%= t('site.current.keywords') %><%end%>
<%content_for :description do%><%= (@category.description ? @category.description : "") %><%end%>
<%content_for :alternate_urls do%><%=raw alternate_urls(polymorphic_url(@category), @category)%><%end%>
<%content_for :canonical do%><%= polymorphic_url(@category, :locale => params[:locale]) %><%end%>

<%= render :partial => 'application/breadcrumb_path', :locals  => { :path => @category.parents_path, :categorycrumb => true } %>

<section id="category">
  <div class="category_info">
    <div class="left_info">
        <!-- corner -->
      <div class="corner-item">
        <div class="corner-item2">
          <div class="corner-color"></div>
          <i class="fa fa-th-large"></i>
        </div>
        <div class="corner-background"></div>
      </div>
    
      <div class="container-img">
         <%= render :partial => 'categories/thumbnail', :locals =>{:category => @category, :show_avatar => true} %>
      </div>
    </div>

    <div class="information">
      <div class="briefing">
        <header>
          <div class="icontitle">
            <% if user_signed_in? and @category.owner.user == current_subject %>
              <%= render partial: 'help/help_category_own' %>
            <% else %>
              <%= render partial: 'help/help_category' %>
            <% end %>
            <h1><%= @category.title %></h1>
            <div class="description"><%= @category.description %></div>
            <div class="author"> <span class="by"><%= t('by') %></span> <%= link_to @category.author.name, @category.author.subject %></div>
          </div>
        </header>
        <div class="toolbar-profile">
          <div class="btn-group js_update_profile_info">
          <% if can?(:update, @category) %>
            <%= link_to(raw("<i class='fa fa-edit '></i>")+ t('excursion.actions.edit'), '#', :id => "edit_icon_link", :class =>"btn btn-default edit_icon") %>
            <a class="btn btn-default btn-for-categorize"><i class="fa fa-arrows"></i><%=t('categories.dragandrop.place')%></a>
          <% end %>
          <% if can?(:destroy, @category) %>
            <%= link_to(raw("<i class='fa fa-trash'></i>") + t('excursion.actions.delete') , @category, :method => :delete, :remote => false, :confirm => "Are you sure?", :class =>"action btn btn-default" )%>
          <% end %>
          </div>
        </div>
      </div>
    </div>
    
    <% if can?(:update, @category) %>
      <div class="update js_update_profile_info" style="display:none">
        <%= form_for(@category, :url => [@category]) do |f| %>
          <%= f.hidden_field :owner_id %>
          <label><%= t('categories.leyend.title')%></label>
          <%= f.text_field :title, :class => "form-control"  %>
          <label><%= t('categories.leyend.description')%></label>
          <%= f.text_field :description, :class => "form-control"  %>
          <label><%= f.label :parentCategory, t('categories.actions.belongs_to') %></label>
          <%= f.select(:parent_id, options_for_select(Category.category_parents_options_for_select(current_subject,@category), @category.parent.nil? ? nil : @category.parent.id)) %>
          <%= f.label :scope, t('document.leyend.scope') %>
          <%= f.select(:scope, options_for_select([[t('scope.public'), 0], [t('scope.private'), 1]],@category.scope)) %>
          <label><%= f.label :avatar, t('document.leyend.avatar') %></label>
          <%= f.file_field :avatar %>
            <!--Fix styles-->
          <div class="actions">
            <%= f.submit t('categories.save'), :id => "saveCategory", :class=>"btn btn-primary" %>
            <%=link_to t('hide'), "#", :id=>"hide_category_form", :class=>"btn btn-default" %>
          </div>
        <% end %>
      </div>
    <% end %>
    
  </div>
</section>

<%  @items = @category.property_objects
    order = @category.categories_order
    unless order.nil?
      orderLength = order.length
      @items.sort_by!{|h| order.index(h.id) || (orderLength+1)}
    end %>

<% if can?(:update, @category) %>
  <%= render partial: 'categories/edit_categories', :locals => { :all_categories => @items} %>
<% end %>


<div class="main-categories-space">
  <ul class="items">
    <% if can?(:update, @category) %>
      <%= render partial: 'categories/category-add' %>
    <% end %>
      <%= render partial: 'categories/items' %>
  </ul>
</div>

<%= content_for :javascript do %>
    $(".edit_icon").on("click", function(){
      $(".update").show("slow");
      $(".btn-group js_update_profile_info").hide("slow");
      $(".information").hide("slow");
    });

    $("#hide_category_form").on("click", function(){
      $(".update").hide("slow");
      $(".btn-group js_update_profile_info").show("slow");
      $(".information").show("slow");
    });

<% end %>

<script>
    var beginIndex;
    var beginObjects;
    Array.prototype.equals = function (array) {
        // if the other array is a falsy value, return
        if (!array)
            return false;

        // compare lengths - can save a lot of time
        if (this.length != array.length)
            return false;

        for (var i = 0, l=this.length; i < l; i++) {
            // Check if we have nested arrays
            if (this[i] instanceof Array && array[i] instanceof Array) {
                // recurse into the nested arrays
                if (!this[i].equals(array[i]))
                    return false;
            }
            else if (this[i] != array[i]) {
                // Warning - two different object instances will never be equal: {x:20} != {x:20}
                return false;
            }
        }
        return true;
    };

  function updateMainView(){
      var actualState = $(".edit_categories .box-item").clone();
      var s = $(".main-categories-space .box-item");
      <% if @category.owner.user == current_subject %>
        s.splice(0,1);
      <% end %>
      s.each(function( index ) {this.remove();});
      actualState.appendTo(".main-categories-space ul");
  };

</script>

<% content_for :javascript do %>
    
    //Let the overlay appear
      
    $(".btn-for-categorize").click(function(){
      $("#categories_edition").fadeIn();
      beginIndex = getidArray();
      beginObjects = $(".edit_categories .box-item").clone();        
    });

    //Apply authomatically the initial category to be created as the one you are at
    $("#AddCategoryHeadercategory_parent_id").val(<%=@category.id%>); 

<% end %>
